package month5.day8;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class Server {
    private static List<Socket> socketList = new ArrayList<>();

    public static void main(String[] args) {
        try {
            startupServer(8080);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void startupServer(int port) throws IOException {
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("聊天室已开启");
        while (true) {
            //等待客户端连接
            Socket client = serverSocket.accept();
            //把客户端IP和端口加入集合
            socketList.add(client);
            //获得客户端端口
            int remotePort = client.getPort();
            //获得客户端IP
            String remoteIP = client.getInetAddress().getHostAddress();
            System.out.println(remoteIP + remotePort + "加入服务器");
            //开启线程
            new Thread(new ServerThreadHandler(socketList,client)).start();

        }
    }
    //内部类开启多线程 每连接一个客户端开启一个线程
    public static class ServerThreadHandler implements Runnable {
        private List<Socket> socketList;
        private final Socket socket;

        public ServerThreadHandler(List<Socket> socketList, Socket socket) {
            this.socketList = socketList;
            this.socket = socket;
        }

        @Override
        public void run() {
            System.out.println("当前线程对象名称" + Thread.currentThread().getName() + "为" + this.socket.getInetAddress().getHostAddress() + ":" + this.socket.getPort() + "服务");
            BufferedReader bufferedReader = null;
            try {
                bufferedReader = new BufferedReader(new InputStreamReader(this.socket.getInputStream(), StandardCharsets.UTF_8));
                //读取客户端信息
                String msg = bufferedReader.readLine();
                //发送给所有客户端
                dispatch(msg);
            } catch (Exception e) {
                String remoteIP = this.socket.getInetAddress().getHostAddress();
                int port = socket.getPort();
                System.out.println(remoteIP + port + "已离开");
                //从集合中删除该线程信息
                socketList.remove(socket);
                if (!socket.isClosed()) {
                    try {
                        socket.close();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                }
                System.out.println("当前聊天室人数:"+socketList.size());
            }
        }
        //给每个客户端发消息
        private void dispatch(String msg) {
            PrintWriter printWriter = null;
            //集合必须存在且不能为空
            if (socketList == null || socketList.size() == 0) {
                return;
            }

        }
    }
}
